package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.CarMapper;
import com.wanghaixin.dao.CarParamTypeMapper;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.CarParamType;
import com.wanghaixin.pojo.CarParamTypeExample;
import com.wanghaixin.service.CarParamTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/2 09:23
 * @Description:
 */
@Service
public class CarParamTypeServiceImpl implements CarParamTypeService {

    @Autowired
    private CarParamTypeMapper carParamTypeMapper;

    @Override
    public int addCarParam(CarParamType carParamType) {
        int i = carParamTypeMapper.insertSelective(carParamType);
        return i;
    }

    @Override
    public void delCarParam(Integer id) {
        int i = carParamTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int upCarParam(CarParamType carParamType) {
        int update = carParamTypeMapper.updateByPrimaryKeySelective(carParamType);
        return update;
    }

    @Override
    public CarParamType findById(Integer id) {
        CarParamType carParamType = carParamTypeMapper.selectByPrimaryKey(id);
        return carParamType;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        
        CarParamTypeExample example = new CarParamTypeExample();
        String sort = queryDTO.getSort();
        if (!StringUtils.isEmpty(sort)) {
            example.setOrderByClause("id");
        }
        List<CarParamType> carParamTypes = carParamTypeMapper.selectByExample(example);

        PageInfo<CarParamType> pageInfo = new PageInfo<>(carParamTypes);
        long total = pageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, carParamTypes);
        return dataGridResult;
    }

    @Override
    public List<CarParamType> findAll() {
        List<CarParamType> carParamTypes = carParamTypeMapper.selectByExample(null);
        return carParamTypes;
    }
}
